local data_strings = {"Nalorakk", "Akil'zon", "Attumen"}
local scriptversion = "0.1.0"
local datavesion = "0.1.0"
local outofdate = false;
local receivingdata = false;
local _recnr = 1;
LoadVars();

local function GetArgs(text, separator)
	local args = {}
	local i = 0
	for value in string.gmatch(text, "[^"..separator.."]+") do
		i = i + 1
		args[i] = value
	end	
	return args
end

function Reset()
	ETacsMain:SetWidth(900)
	ETacsMain:SetHeight(500)
	ETacsMain:SetUserPlaced(false)
end

function LoadVars() 
	data = {
	-- Zul'Aman
	Nalorakk = "Zul'Aman > Nalorakk (Bear Avatar) > Version: 0.1.0 |n|nBasic information:|n- The boss has 2 phases: Humanoid (lasts 45 seconds) and Bear (lasts 20 seconds).|n- All attacks are physical.|n- Ranged party members should stand at max range and spread out as the boss has an AoE charge attack during the Humanoid phase.|n- Positioning is the same for both phases, so once everyone is positioned, no further movement is needed.|nNote: This boss is resettable; simply run down the steps from where you entered the boss' platform. If one tank dies, the boss should be reset immediately|n|nTanks:|n- Tanks must be standing side by side because the cleave damage is distributed: 7,500 on both tanks instead of 15,000 on a single tank.|n- Humanoid phase: Tank1 is tanking the boss (taunt Nalorakk after the bear phase). Tank2 MUST stay below Tank1' threat.|n- Bear phase: Tank2 taunts the boss in the beginning of the phase and keeps aggro through the bear phase|n|nHealers:|n- Beware of a random AOE silence.|n- Keep the tanks at max health and use heal-over-time spells.|n- Humanoid phase: There should be 2 healers on the tanks, and one raid/off-healer. Keep an eye on the on the offtank as the boss does cleaves.|n- Bear phase: ALL healers should be on the tank. The damage dealt in this phase is extremely high.|n|nDPS:|n- Ranged: Stand at max range and spread out.|n- Melee: Stand behind the boss.|n|nRacial:|n- Dwarfs: Bleed effects can be removed by the dwarf racial ability Stoneform.",
	Akilzon = "Zul'Aman > Akil'zon (Lynx Avatar) > Version: 0.1.0 |n|ntest1|ntest2|ntest3|ntest4",
	-- Karazhan
	Attumen = "Karazhan > Attumen the Huntsman & Midnight > Version: 0.1.0 |n|ntest|ntesttest"}
end

function ETacs_SlashHandler(msg)
	local msgargs = GetArgs(msg," ");
	if (msg=="") then
		--DEFAULT_CHAT_FRAME:AddMessage("***LFGVoyeur Help***")
		--DEFAULT_CHAT_FRAME:AddMessage("/lfgv ...")
		--DEFAULT_CHAT_FRAME:AddMessage("  show   = Show main window")
		--DEFAULT_CHAT_FRAME:AddMessage("  reset  = Reset window and restart UI")
		--DEFAULT_CHAT_FRAME:AddMessage("  normal = Normal instances only")
		--DEFAULT_CHAT_FRAME:AddMessage("  heroic = Heroic instances only")
		--DEFAULT_CHAT_FRAME:AddMessage("  both   = Normal and heroic instances both")
		--DEFAULT_CHAT_FRAME:AddMessage("  min    = Set minimum players per instance (currently " .. LFGVoyeur_Min .. ")")
	end

	if (msgargs[1]=="show") then
		local boss = CheckForBossName(msg);
		if (not (boss == false)) then	
			ShowData(boss);
		else DEFAULT_CHAT_FRAME:AddMessage("ETacs: The boss wasn't found in the database"); end
	end

	if (msg=="reset") then
		Reset();
	end
	if (msg=="restore") then
		LoadVars();
	end
end

function CheckForBossName(text)
	local temp_step = 1;
	while (temp_step <= #(data_strings)) do
		if (not (string.find(strlower(gsub(text, "'", "")), strlower(gsub(data_strings[temp_step], "'", ""))) == nil)) then
			return data_strings[temp_step]
		end
		temp_step = temp_step + 1;
	end
	return false
end

function SendData(boss, channel, sender, offset)
	local tempstring = data[boss];
	local part
	if (offset > 200) then part = (offset-1)/200; else part = 0; end
	if ((strlen(tempstring)-offset) <= 200) then 
		if (part == 0) then
			SendAddonMessage( "ETacs_data " .. boss .. " " .. part, tempstring, channel, sender );
		else
			SendAddonMessage( "ETacs_data_split " .. boss .. " " .. part, string.sub(tempstring, offset, strlen(tempstring)), channel, sender );
		end
	else
		SendAddonMessage( "ETacs_data_split " .. boss .. " " .. part, string.sub(tempstring, offset, (offset+200)), channel, sender ); -- 237 + 17 + channel + name = 266
		SendData(boss, channel, sender, (offset+200))
	end
end

function CheckForMoreData(sender)
	if (_recnr < #data_strings) then
		_recnr = _recnr + 1;
		SendAddonMessage( "ETacs_requestdata", data_strings[_recnr], "WHISPER", sender );
	else
		receiving = false;
	end
end

function ShowData(boss)
	ETacsMainText:SetText(data[gsub(boss, "'", "")])
	ETacsMain:Show()
end

function ETacs_Main(event)
	local boss = false
	local channel = false;
	if ((event == "CHAT_MSG_WHISPER") or (event == "CHAT_MSG_RAID") or (event == "CHAT_MSG_PARTY")) then
		if (event == "CHAT_MSG_WHISPER") then channel = "WHISPER" end
		if (event == "CHAT_MSG_RAID") then channel = "RAID" end
		if (event == "CHAT_MSG_PARTY") then channel = "PARTY" end
		if (not (channel == false)) then
			local argum = GetArgs(arg1," ")
			if (argum[1] == "!etacs") then 
				boss = CheckForBossName(arg1);
				if (not (boss == false)) then	
					SendData(gsub(boss, "'", ""), channel, arg2, 1);
					SendAddonMessage( "ETacs_showboss", gsub(boss, "'", ""), channel, arg2 );
				else SendChatMessage("The boss was not found in the database", channel, GetDefaultLanguage("player"), arg2); end
			end
		end
	elseif (event == "CHAT_MSG_ADDON") then
		local prefix = GetArgs(arg1," ");
		boss = prefix[2];
		local part = prefix[3];
		local channel = arg3;
		if (prefix[1] == "ETacs_data") then 
			data[boss] = arg2;
			CheckForMoreData(arg4);
		end
		if (prefix[1] == "ETacs_data_split") then 
			if (part == 0) then data[boss] = arg2; 
			else data[boss] = data[boss] .. arg2; end
			CheckForMoreData(arg4);
		end
		if (prefix[1] == "ETacs_showboss") then
			boss = CheckForBossName(arg2);
			if (not (boss == false)) then
				local message = arg4 .. " wants to show you the tactics for " .. boss .. ". Do you want to open the window?";
				local dialog = StaticPopup_Show("ETACS_ACCEPT_SHOW", message)
				if(dialog) then
					dialog.data = boss;
				end
			end
		end
		if (prefix[1] == "ETacs_versioncheck") then
			SendAddonMessage( "ETacs_version", scriptversion .. " " .. dataversion, channel, arg4 );
		end
		if (prefix[1] == "ETacs_version") then
			local versionargs = GetArgs(arg2, " ");
			local _scriptversion = versionargs[1];
			local _dataversion = versionargs[2];
			if (_scriptversion > scriptversion) then
				outofdate = true;
			end
			if ((_dataversion > dataversion) and (receivingdata == false)) then 
				_recnr = 1;
				SendAddonMessage( "ETacs_requestdata", data_strings[_recnr], "WHISPER", arg4 );
				receivingdata = true;
			end
		end
		if (prefix[1] == "ETacs_requestdata") then
			SendData(arg2, channel, arg4, 1)
		end
	end
end